package chapter19.ReaderAndWriter;

import org.junit.jupiter.api.Test;

import java.io.FileWriter;
import java.io.IOException;

/**
 * @Auther: Tx
 * @Date: 2022/7/23 21:32
 */
//字符输出流
public class FileWriter1 {
    @Test
    void test1() {
        String filePath = "e://writer.txt";
        FileWriter fileWriter = null;
        char[] chars = {'a','b','c'};
        try {
            fileWriter = new FileWriter(filePath,true);//追加方式
            fileWriter.write('H');
            fileWriter.write(chars);
            fileWriter.write("hsp教育".toCharArray(),0,3);
            fileWriter.write("你好北京");
            fileWriter.write("上海天津",0,2);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
//            这里注意要关闭流，因为write方法关闭流之后才会写入
            try {
                fileWriter.close();
                System.out.println("输出成功");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
